home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / debugc.arc / DEBUG.C
Text File  |  1986-03-13  |  11KB  |  334 lines

  1. <TAU>DEBUG.H:    
  2. 50 lines
  3. /*----------------------------DEBUG.H--------------------------------------
  4. *  DEBUG.H  This header file contains the statements necessary for the
  5. *           conditional compilation of debug statements, and a typedef
  6. *           us
  7. ed by the inkey() function.
  8. *------------------------------------------------------------------------*/
  9. extern int trace_sw;                     /*trace_sw is defined in debug.c*/
  10.  
  11. #define DEBUG
  12.  
  13. #ifdef DEBUG
  14. #define GENERATE_STATEMENTS(statements) statem
  15. ents
  16. #else
  17. #define GENERATE_STATEMENTS(statements) /*empty*/
  18. #endif
  19.  
  20. /*trace a character variable*/
  21. #define TC(user_text,character_variable) \
  22. GENERATE_STATEMENTS(if (trace_sw) t_c(user_text,character_variable))
  23.  
  24.  
  25. /*trace an integer variable*/
  26. #define TI(user_text,integer_variable) \
  27. GENERATE_STATEMENTS(if (trace_sw) t_i(user_text,integer_variable))
  28.  
  29. /*trace a long variable*/
  30. #define TL(user_text,long_variable)
  31. \
  32.     GENERATE_STATEMENTS(if (trace_sw) t_l(user_text,long_variable))
  33.  
  34. /*trace an unsigned variable*/
  35. #define TU(user_text,unsign_variable) \
  36. GENERATE_STATEMENTS(if (trace_sw) t_u(user_text,unsign_variable))
  37.  
  38.  
  39. /*trace a double variable*/
  40. #define TD(user_text,double_variable) \
  41. GENERATE_STATEMENTS(if (trace_sw) t_d(user_text,double_variable))
  42.  
  43. /*trace a float variablle*/
  44. #define TF(user_text,float_variable) \
  45. G
  46. ENERATE_STATEMENTS(if (trace_sw) t_f(user_text,float_variable))
  47.  
  48. /*trace a string variable*/
  49. #define TS(user_text,string_variable) \
  50. GENERATE_STATEMENTS(if (trace_sw) t_s(user_text,string_variable))
  51.  
  52. typedef struct         
  53. /*structure used by the inkey() keyboard function*/
  54. {
  55.     char char1;      /*inkey() returns a character, or zero into this field*/
  56.     char char2;  /*if char1 is zero, inkey() returns the extended code here*/
  57. }
  58. TKB;
  59.  
  60. /*----------------------------END DEB
  61. UG.H--------------------------------*/
  62. <TAU>DEBUG.C:    
  63. 176 lines
  64. /*-------------------------DEBUG.C-----------------------------------------
  65. *  DEBUG.C         C language symbolic debugger.
  66. *
  67. *  (c) Copyright 1985 by Thomas D. Webb.
  68. *  All rights reserved.
  69.  
  70. *  Permission is granted for unlimited personal, non-commercial use only.
  71. *
  72. -------------------------------------------------------------------------*/
  73. #include <stdio.h>
  74. #include <debug.h>
  75. extern int CRTBUF();     /*external routine saves and restores cr
  76. t buffer*/
  77. #define CONTROL_KEY 68 /*fkey 10 used to toggle trace display on and off*/
  78. #define VOID
  79. #define ON 1
  80. #define OFF 0
  81. #define TRUE 1
  82. #define FALSE 0
  83. #define SAVE    0                       /*Used by CRTBUF*/
  84. #define REPLACE 1                      
  85. /*Used by CRTBUF*/
  86. #define BLANK  ' '
  87. #define MAX_ROWS 24
  88. #define MAX_COLUMNS 80
  89. #define BUFFER_SIZE 4000         /*size of crt buffer save area*/
  90. int trace_sw = OFF;                    /*turns trace table display on/off*/
  91. static int first_time = TRUE;
  92. c
  93. har user_data[MAX_COLUMNS / 2];      /*users data, in trace table format*/
  94. typedef struct tchr_attr          /*structure used to address the video..*/
  95. {                               /*..character and attribute bytes*/
  96.     char chr;
  97.     char attr;
  98.  
  99. }
  100. TCHR_ATTR;
  101. static char save_buffer[BUFFER_SIZE];              /*crt buffer save area*/
  102. static TCHR_ATTR trace_table[MAX_ROWS+2][MAX_COLUMNS];  /*the trace table*/
  103. /*-----------------------------------------------------------------------*/
  104. TKB *inkey()
  105. /*bdos call to keyboard buffer returns a character..*/
  106. {                 /*..in kb.char1, or, if kb.char1 is binary zero, */
  107.     /*..an extended code in kb.char2*/
  108. #define DOSFUNCTION 0x7        /* 0x7 = console input with echo*/
  109. #def
  110.     ine DOSPARM     0x0        /* no parm is required */
  111.     TKB kb_rec;                 /*two-character work area used to receive..*/
  112.     /*..the characters from the keyboard*/
  113.     kb_rec.char1 = kb_rec.char2 = NULL;
  114.     kb_rec.char1 = bdos(DOS
  115.     FUNCTION,DOSPARM);
  116.     if (kb_rec.char1 == 0)                           /*IBM PC extended code*/
  117.     {
  118.     kb_rec.char2 = bdos(DOSFUNCTION,DOSPARM);
  119.     if (kb_rec.char2 == CONTROL_KEY) /*key that controls the trace*/
  120.         (trace_sw == ON) ? (t
  121.         race_sw = OFF) : (trace_sw = ON);
  122.     }
  123.     return(&kb_rec);
  124. }/*inkey*/
  125. /*-----------------------------------------------------------------------*/
  126. VOID wait()                                /* routine to cause a wait */
  127. {                                  /*
  128. waits for the user to press a key*/
  129.     inkey();                                /*the key value is discarded*/
  130. } /* wait */
  131. /*-----------------------------------------------------------------------*/
  132. VOID init_table()                           /*initialize 
  133. the trace_table*/
  134. {
  135.     int i,j;
  136.     static char trace_msg[] =
  137.     {
  138.     "Trace table...Press any key to return...Press F10 to suppress trace"    };
  139.     for (i = 0; i < MAX_ROWS +2; i++)
  140.     for (j = 0; j < MAX_COLUMNS; j++)
  141.     {
  142.         trace_table[i][j
  143.         ].chr = BLANK;
  144.         trace_table[i][j].attr = 0x7;
  145.     }
  146.     for (i = 0; i < sizeof(trace_msg); i++)
  147.     trace_table[0][i].chr = trace_msg[i];
  148. } /* init_table */
  149. /*-----------------------------------------------------------------------*/
  150. VO
  151. ID display_trace(user_text,user_data)   /* display the trace table */
  152. char user_text[];
  153. char user_data[];
  154. {
  155.     int i,j;
  156.     if (first_time)                           /*initialize the trace table*/
  157.     {
  158.     init_table();
  159.     first_time = FALSE;
  160.  
  161.     }
  162.     movmem(&trace_table[2],&trace_table[1],         /*bump the trace table*/
  163.     sizeof(trace_table) - 160);
  164.     for (i = 0; i < MAX_COLUMNS; i++)       /*init last row of trace table*/
  165.     {
  166.     trace_table[MAX_ROWS][i].chr = BLANK;
  167.  
  168.     trace_table[MAX_ROWS][i].attr = 0x7;         /*normal attribute*/
  169.     }                                /*move user string to trace table*/
  170.     for (i = 0; (i < MAX_COLUMNS / 2) && (user_text[i] != NULL); i++)
  171.     trace_table[MAX_ROWS][i].chr = us
  172.         er_text[i];
  173.     for (i = MAX_COLUMNS / 2,j = 0;        /*move user data to trace table*/
  174.         (i < MAX_COLUMNS) && (user_data[j] != NULL); i++,j++)
  175.         trace_table[MAX_ROWS][i].chr = user_data[j];
  176.     CRTBUF(SAVE,save_buffer);                /* save 
  177. the user's crt buffer */
  178.     CRTBUF(REPLACE,trace_table);                /* display the trace table */
  179.     wait();                     /*wait while the user views the trace table*/
  180.     CRTBUF(REPLACE,save_buffer);           /*restore the user's crt buffer */
  181.  
  182. } /* display_trace */
  183. /*-----------------------------------------------------------------------*/
  184. VOID t_c(user_text,character_variable)   /*trace a character*/
  185. char user_text[];
  186. char character_variable;
  187. {
  188.     if (character_variable == BLANK)
  189.     strcpy(
  190.     user_data,"BLANK char");
  191.     else if (character_variable == NULL)
  192.     strcpy(user_data,"NULL char");
  193.     else sprintf(user_data,"%c",character_variable);
  194.     display_trace(user_text,user_data);
  195. }/*t_c*/
  196. /*-------------------------------------------------
  197. ----------------------*/
  198. VOID t_i(user_text,integer_variable)    /*trace an integer*/
  199. char user_text[];
  200. int integer_variable;
  201. {
  202.     sprintf(user_data,"%d",integer_variable);
  203.     display_trace(user_text,user_data);
  204. }/*t_i*/
  205. /*--------------------------------
  206. ---------------------------------------*/
  207. VOID t_l(user_text,long_variable)    /*trace a long integer*/
  208. char user_text[];
  209. long int long_variable;
  210. {
  211.     sprintf(user_data,"%ld",long_variable);
  212.     display_trace(user_text,user_data);
  213. }/*t_i*/
  214. /*--------------
  215. ---------------------------------------------------------*/
  216. VOID t_u(user_text,unsign_variable)    /*trace an unsigned integer*/
  217. char user_text[];
  218. unsigned unsign_variable;
  219. {
  220.     sprintf(user_data,"%u",unsign_variable);
  221.     display_trace(user_text,user_data
  222.     );
  223. }/*t_u*/
  224. /*-----------------------------------------------------------------------*/
  225. VOID t_f(user_text,float_variable)    /*trace a float*/
  226. char user_text[];
  227. double float_variable;
  228. {
  229.     sprintf(user_data,"%f",float_variable);
  230.     display_trace(user_tex
  231.     t,user_data);
  232. } /* t_f */
  233. /*-----------------------------------------------------------------------*/
  234. VOID t_d(user_text,double_variable)    /*trace a double*/
  235. char user_text[];
  236. double double_variable;
  237. {
  238.     sprintf(user_data,"%f",double_variable);
  239.     displa
  240.     y_trace(user_text,user_data);
  241. } /* t_d */
  242. /*-----------------------------------------------------------------------*/
  243. VOID t_s(user_text,string_variable)    /*trace a string*/
  244. char user_text[];
  245. char string_variable[];
  246. {
  247.     int i, all_blanks;
  248.     if (string_v
  249.     ariable[0] == NULL)    /*if string starts with a null char*/
  250.     {
  251.     strcpy(user_data,"NULL STRING");
  252.     display_trace(user_text,user_data);
  253.     return;
  254.     }
  255.     for (i = 0,all_blanks = TRUE;(i < MAX_COLUMNS /2) &&
  256.         (string_varia
  257. ble[i] != NULL);i++)
  258.     {
  259.     user_data[i] = string_variable[i];           /*move in the string*/
  260.     if (string_variable[i] != BLANK) all_blanks = FALSE;
  261.     }
  262.     if (all_blanks)
  263.     strcpy(user_data,"BLANK STRING");
  264.     else user_data[
  265.     i] = NULL;
  266.  
  267.     display_trace(user_text,user_data);
  268. } /* t_s */
  269. /*-------------------------END DEBUG.C-----------------------------------*/
  270. <TAU>DRIVER.C:    
  271. 54 lines
  272. /*------------------------------DRIVER.C-----------------------------------
  273. *  DRIVER.C  Rou
  274. tines to illustrate the use of the symbolic debugger.
  275. *
  276. *
  277. *------------------------------------------------------------------------*/
  278. #include <stdio.h>
  279. #include <debug.h>
  280. /*-----------------------------------------------------------------------*/
  281. func2()
  282. /* this illustrates a typical application of the debugger: */
  283. {             /* 1. record the entry into a function */
  284.     /* 2. record the program variables as they change */
  285.     /* 3. record the exit from the function */
  286.     in
  287.     t small_number;
  288.     long big_number;
  289.     TS("enter func2","-------------------");
  290.     for (small_number = 0, big_number = 0L; small_number < 5; small_number++)
  291.     {
  292.     big_number += small_number *10;
  293.     TI("small_number:",small_number);
  294.  
  295.     TL("big_number:", big_number);
  296.     }
  297.     TS("exit func2","^^^^^^^^^^^^^^^^^^^");
  298.  
  299. } /*func2*/
  300. /*-----------------------------------------------------------------------*/
  301. func1()            /*illustrate the use of all of the types of traces*/
  302. {
  303.  
  304.     char a = 'A';
  305.     int j = 123;
  306.     int *j_ptr = &j;
  307.     long k = 456789;
  308.     float f = 12.978;
  309.     static char string[] = "hello, world";
  310.     TS("enter func1","-------------------");
  311.     TC("Character test",a);
  312.     TI("Integer test",j);
  313.     TL("Long test",k);
  314.  
  315.     TU("Unsigned test",j_ptr);
  316.     TF("Float test",f);
  317.     TD("Double test",123456.789012);
  318.     TS("String test",string);
  319.     TS("exit func1","^^^^^^^^^^^^^^^^^^^^");
  320. } /*func1*/
  321. /*-----------------------------------------------------------------------*/
  322. main()
  323. {
  324.  
  325.     printf("Debug driver version 02 start\n");
  326.     printf("Press F10 to start trace\n");
  327.     inkey();
  328.     func1();
  329.     func2();
  330.     printf("Debug driver end\n");
  331. } /*main*/
  332. /*--------------------------END DRIVER.C---------------------------------*/
  333.  
  334.